package com.tnr.scgcxx.service.safty.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tnr.scgcxx.dao.safty.UserDao;
import com.tnr.scgcxx.dto.safty.RoleDto;
import com.tnr.scgcxx.dto.safty.UserDto;
import com.tnr.scgcxx.model.safty.Role;
import com.tnr.scgcxx.model.safty.User;
import com.tnr.scgcxx.service.safty.UserService;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public UserDto findUser(String id,String pwd) {
        User user = new User();
        user.setU_id(id);
        user.setU_pwd(pwd);
        User admin = userDao.findUser(user);
        if (admin!=null){
            UserDto userDto = new UserDto();
            userDto.setU_id(admin.getU_name());
            userDto.setU_name(admin.getU_name());
            userDto.setU_status(admin.getU_status());
            return userDto;
        }else {
            return null;
        }
    }

	@Override
	public List<User> findUsers(User user) {
		
		return userDao.findUsers(user);
	}

	@Override
	public int addUser(User user) {
		
		return userDao.addUser(user);
	}

	@Override
	public int deleteUserByIds(String[] u_ids) {
		
		return userDao.deleteUserByIds(u_ids);
	}

	@Override
	public int updateUserStatusByIds(String[] u_ids, String u_status) {
	
		return userDao.updateUserStatusByIds(u_ids,u_status);
	}

	@Override
	public int updateUser(User user) {
		return userDao.updateUser(user);
	}

	@Override
	public int deleteUserById(String u_id) {
		
		return userDao.deleteUserById(u_id);
	}

	@Override
	public User findUserById(String id) {
		return userDao.findUserById(id);
	}

	@Override
	public List<RoleDto> getUserRolesById(String u_id) {
		List<Role> list = userDao.getUserRolesById(u_id);
		List<RoleDto> rolelist = new ArrayList<RoleDto>();
		for (Role role : list) {
			RoleDto roleDto = new RoleDto();
			roleDto.setRo_id(role.getRo_id());
			roleDto.setRo_name(role.getRo_name());
			roleDto.setRo_check(role.getU_id()!=null);
			rolelist.add(roleDto);
		}
		return rolelist;
	}

	@Override
	public void setUserRoles(Integer[] ro_ids, String u_id) {
		userDao.deleteURByUid(u_id);
		if(ro_ids.length!=0){
			userDao.addURs(ro_ids,u_id);
		}
	}

	

    

}
